package com.xdl.moudle.utils.util;

import java.io.UnsupportedEncodingException;
import java.util.Random;

/**
 * 汉子随机生成
 * 
 * @author xiazhengjiao
 * @date 2017年9月2日
 */
public class RandomUtil {
	private static String[] charSet = new String[] { "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m",
			"n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z", "A", "B", "C", "D", "E", "F", "G", "H",
			"I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z", "1", "2", "3",
			"4", "5", "6", "7", "8", "9", "0" };

	private static int size = 62;
	private static Random random = new Random();

	public static String getRandomStr(int length) {
		StringBuffer buff = new StringBuffer();
		for (int i = 0; i < length; i++) {
			buff.append(charSet[random.nextInt(size)]);
		}
		return buff.toString();
	}

	private static String getChinese() {
		String str = null;
		int highPos, lowPos;
		highPos = (176 + Math.abs(random.nextInt(39)));
		lowPos = 161 + Math.abs(random.nextInt(93));
		byte[] b = new byte[2];
		b[0] = (new Integer(highPos)).byteValue();
		b[1] = (new Integer(lowPos)).byteValue();
		try {
			str = new String(b, "GB2312");
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
		return str;
	}

	public static String getFixedLengthChinese(int length) {
		String str = "";
		for (int i = length; i > 0; i--) {
			str = str + getChinese();
		}
		return str;
	}

	public static String getRandomLengthChiness(int start, int end) {
		String str = "";
		int length = new Random().nextInt(end + 1);
		if (length < start) {
			str = getRandomLengthChiness(start, end);
		} else {
			for (int i = 0; i < length; i++) {
				str = str + getChinese();
			}
		}
		return str;
	}

	/**
	 * 生成6位随机数
	 * 
	 * @return
	 */
	public static int getRandomSix() {
		return (int) ((Math.random() * 9 + 1) * 100000);
	}

	/**
	 * 生成5位随机数
	 * 
	 * @return
	 */
	public static int getRandomFive() {
		return (int) ((Math.random() * 9 + 1) * 10000);
	}

	/**
	 * 生成4位随机数
	 * 
	 * @return
	 */
	public static int getRandomFour() {
		return (int) ((Math.random() * 9 + 1) * 1000);
	}

	/**
	 * 成在[min,max]之间的随机整数
	 * 
	 * @param min
	 * @param max
	 * @return
	 */
	public static int getRandomNextInt(int min, int max) {
		Random random = new Random();
		int s = random.nextInt(max) % (max - min + 1) + min;
		return s;
	}
}
